package com.maiworld.seller.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maiworld.mapper.ReservationMapper;
import com.maiworld.mapper.TbCoachMapper;
import com.maiworld.mapper.TbEvaluationMapper;
import com.maiworld.mapper.TbReleaseNewsMapper;
import com.maiworld.mapper.TbReservationMapper;
import com.maiworld.pojo.TbCoach;
import com.maiworld.pojo.TbReleaseNews;
import com.maiworld.pojo.TbReservation;
import com.maiworld.pojo.TbReservationExample;
import com.maiworld.pojo.TbReservationExample.Criteria;
import com.maiworld.seller.service.ReservationService;

import entity.PageResult;
@Service
public class ReservationServiceImpl implements ReservationService {

	@Autowired
	private TbReservationMapper tbReservationMapper;
	
	@Autowired
	private ReservationMapper reservationMapper;
	
	@Autowired
	private TbEvaluationMapper tbEvaluationMapper;
	
	@Autowired
	private TbReleaseNewsMapper tbReleaseNewsMapper;
	
	@Autowired
	private TbCoachMapper tbCoachMapper;
	
	@Override
	public List<TbReservation> findAll() {
		
		return tbReservationMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<TbReservation> page = (Page<TbReservation>) tbReservationMapper.selectByExample(null);
		
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbReservation reservation) {
		tbReservationMapper.insert(reservation);

	}

	@Override
	public void update(TbReservation reservation) {
		tbReservationMapper.updateByPrimaryKey(reservation);

	}

	@Override
	public TbReservation findOne(Long id) {
		return tbReservationMapper.selectByPrimaryKey(id);
	}

	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			tbReservationMapper.deleteByPrimaryKey(id);
		}
		
	}

	@Override
	public PageResult findPageReservation(TbReservation reservation, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		TbReservationExample example = new TbReservationExample();
		Criteria create = example.createCriteria();
		if(reservation!=null){
			if(reservation.getCheckStatus()!=null && reservation.getCheckStatus().length()>0){
				create.andCheckStatusLike("%"+reservation.getCheckStatus()+"%");
			}if(reservation.getSname()!=null && reservation.getSname().length()>0){
				create.andSnameLike("%"+reservation.getSname()+"%");
			}if(reservation.getStatus()!=null && reservation.getStatus().length()>0){
				create.andStatusEqualTo(reservation.getStatus());
			}
		}
		Page<TbReservation> page = (Page<TbReservation>) tbReservationMapper.selectByExample(example);
		return new  PageResult( page.getTotal(), page.getResult());
	}


	@Override
	public PageResult searchListByReservation(TbReservation reservation,int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<Map<String, Object>> page = (Page<Map<String, Object>>) tbReservationMapper.reservationList(reservation);
		return new PageResult(page.getTotal(), page.getResult());
	}

	
	
	
	//获取教练信息
	@Override
	public Map<String,Object> findCoach( Long sid,	String status,String checkStatus) {
		Map<String, Object> result = new HashMap<String, Object>();	

		List<TbCoach> array = new ArrayList<>();
		array = tbReservationMapper.selectBySid(sid,status,checkStatus);

		result.put("statusCode", 200);
		result.put("message", "成功");
		result.put("coachDetails", array);
		return result;	
	}

	//预约但未付款的
	@Override
	public Map<String, Object> findReserId( Long sid,String status, String checkStatus,Long cid){
		
		Map<String, Object> result = new HashMap<String,Object>();
		List<TbReservation> payDetails = new ArrayList<>();
		payDetails = tbReservationMapper.selectByComment(sid, status, checkStatus,cid);
		
		/*TbReservation rese = new TbReservation();
		for (TbReservation a : payDetails) {
			rese.setId(a.getId());
			rese.setStatus(a.getStatus());
			rese.setOpenid(a.getOpenid());
			rese.setCname(a.getCname());
			rese.setPeriod(a.getPeriod());
			rese.setNumber(a.getNumber());
			rese.setSubject(a.getSubject());
			rese.setVehicleType(a.getVehicleType());
			rese.setCheckStatus(a.getCheckStatus());
			rese.setLogoPic(a.getLogoPic());
			rese.setDriveYear(a.getDriveYear());
			rese.setStar(a.getStar());
			rese.setPlateNumber(a.getPlateNumber());
			rese.setPrecent(a.getPrecent());
			if()
		}*/
		
		if(payDetails.isEmpty()) {
			result.put("payDetails",null);
		}else {
			result.put("payDetails",payDetails);
		}		
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
	}

	//学员预约下订单
	@Override
	public Map<String, Object> addRels(Map<String, String> params) {
		Map<String, Object> result = new HashMap<String,Object>();
			
		TbReservation a = new TbReservation();
		a.setSid(Long.parseLong(params.get("sid")));
		a.setCid(Long.parseLong(params.get("cid")));
		a.setReleId(Long.parseLong(params.get("taskId")));
		a.setStatus(params.get("status"));
		List<TbReservation> b = tbReservationMapper.selectByCoachCid(a);
		if(b.size() != 0){
			result.put("statusCode", 500);
			result.put("message", "当前时段已预约");
			return result;
		}
		

		
		TbReleaseNews releaseNews = tbReleaseNewsMapper.selectByPrimaryKey(Long.parseLong(params.get("taskId")));
		Long pNum = releaseNews.getPeopleNum();
		if(pNum == releaseNews.getNumber()){
			result.put("statusCode", 400);
			result.put("message", "学员已满");
		}else	{releaseNews.setPeopleNum(pNum+1);
			tbReleaseNewsMapper.updateByPrimaryKey(releaseNews);	
			
			TbReservation reservation =new TbReservation();
			reservation.setSid(Long.parseLong(params.get("sid")));
			reservation.setCid(Long.parseLong(params.get("cid")));
			reservation.setReleId(Long.parseLong(params.get("taskId")));
			reservation.setStatus(params.get("status"));
			reservation.setCheckStatus("1");
			tbReservationMapper.insert(reservation);									
			result.put("statusCode", 200);
			result.put("message", "预约成功");			
		}
		return result;
	}

		
		
		
		/*Map<String, Object> result = new HashMap<String,Object>();
				
		List<TbCoach> detail = new ArrayList<>();
		detail = (List<TbCoach>) tbReservationMapper.selectBySid(sid);
		
		List<TbCoach> list = (List<TbCoach>) tbReservationMapper.selectBySid(sid);
		//System.out.println("1"+detail.size());
		ArrayList<TbEvaluation> rates = new ArrayList<>();
		 rates=(ArrayList<TbEvaluation>) tbEvaluationMapper.selectByCid();
		
		 ArrayList<TbEvaluation> List = new ArrayList<>();		 
		 TbEvaluation eva = new TbEvaluation();
		
		 for(int i=0; i<detail.size(); i++){
			 for(int j=0; j<rates.size(); j++){
				 if(rates.get(j).getCid().equals( detail.get(i).getCid())){
					 
					 eva.getId();
					 eva.getStar();
					 eva.getContent();
					 eva.getSname();
					 eva.getHeadPic();
					 eva.getCreated();
					 List.add(eva);
				 }else {
					 System.out.println("此教练没有评价");
				}
		 
			}
		}
		 Map<String, Object> ratesRes = new HashMap<String , Object>();
			ratesRes.put("coach_detail", detail);
			if(detail.isEmpty()){
				ratesRes.put("coach_detail", null);
			}else {
				ratesRes.put("coach_detail", detail);
				ratesRes.put("rates", List);
			}
		 	result.put("coach_detail", "ratesRes");
			result.put("statusCode", 200);
			result.put("message","成功");
			return result;*/
		 /*for (Map<String, Object> map : result) {
			Iterator<String> it=  map.keySet().iterator();
		      while(it.hasNext()){
		                String key = it.next();
		                System.out.println(map.get(key));
		      }
		}*/
		/*TbEvaluationExample example = new TbEvaluationExample();
		com.maiworld.pojo.TbEvaluationExample.Criteria create = example.createCriteria();
		create.equals(coach.getCid());
		
		System.out.println(create);
		List<TbEvaluation> array = new ArrayList<>();
		tbEvaluationMapper.selectByCid();*/
		/*System.out.println("3"+result.size());*/
		
		
		
	}




